package top.drinyeit.nongmao.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.base.BaseActivity;
import top.drinyeit.nongmao.presenter.RegistrationPresenter;

/**
 * Created by bigHead on 2017/11/14.
 */

public class RegistrationActivity extends BaseActivity{

    private TextView registration_btn;//注册
    private TextView rules;//用户手则

    private TextView login_phone,get_code,code;
    private RelativeLayout login_tit;
    private ImageView login_back;
    private TextView registration;
    private TextView login_text;

    private RegistrationPresenter registrationPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_registration);
        registration_btn = (TextView) findViewById(R.id.registration_btn);
        rules = (TextView) findViewById(R.id.rules);

        login_phone = findViewById(R.id.login_phone);
        get_code = findViewById(R.id.get_code);
        code = findViewById(R.id.code);
        login_tit = (RelativeLayout) findViewById(R.id.login_tit);
        login_back = (ImageView) findViewById(R.id.login_back);
        registration = (TextView) findViewById(R.id.registration);
        login_text = (TextView) findViewById(R.id.login_text);


        registrationPresenter = new RegistrationPresenter(this);
        init();

        setTitle(true,"注册");
    }

    @Override
    public void init() {
        get_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                registrationPresenter.getCode(login_phone.getText().toString());
            }
        });

        registration_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                registrationPresenter.registration(login_phone.getText().toString(),code.getText().toString());
            }
        });
    }


    Timer timer;
    TimerTask timerTask;
    int s = 60;

    //验证码倒计时
    public void setTime() {
        get_code.setEnabled(false);
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                if (s > 0) {
                    get_code.post(new Runnable() {
                        @Override
                        public void run() {
                            String str = --s + "秒后获取";
                            get_code.setText(str);
                        }
                    });
                } else {
                    if (timerTask != null) {
                        timerTask.cancel();
                    }
                    s = 60;
                    get_code.post(new Runnable() {
                        @Override
                        public void run() {
                            String str = "获取验证码";
                            get_code.setText(str);
                            get_code.setEnabled(true);
                        }
                    });
                }
            }
        };
        timer.schedule(timerTask, 0, 1000);
    }
}
